﻿package com.athena.process
{
	import com.cubeia.firebase.io.protocol.CreateTableRequestPacket;
	import com.cubeia.firebase.io.protocol.Param;
	import com.cubeia.firebase.util.ParameterUtil;
	import fl.containers.ScrollPane; 
	import fl.controls.ScrollPolicy; 
	import flash.display.*;
	import flash.utils.*;
	import flash.text.TextFormat;
	import flash.events.*;
	import com.adobe.serialization.json.JSON;
	import flash.net.URLRequest ;
	import flash.net.URLLoader;
	import flash.net.URLVariables ;
	import flash.net.URLRequestMethod ;
	import flash.net.URLRequestHeader ;
	import com.athena.vo.*;
	import com.athena.controller.*;
	
	/**
	 * ...
	 * @author ...
	 */
	public class ProcessOutTable_Frame2 
	{
		public static var scrTable:ScrollPane = new ScrollPane(); 
		public static var scrollPaneRoom_mc:MovieClip;
		public static var scrRoom:ScrollPane = new ScrollPane(); 
		public static var scrollPaneTable_mc:MovieClip;
		public static var movieRooms : Array = new Array() ;
		public static var movieTables : Array = new Array() ;
		public static var movieTablesXY : Array = new Array() ;
		/******************************************************************************************************/
		/******************************** Functions in Frame 4 ************************************************/
		/******************************************************************************************************/
		public static function TaoKhungFrame2():void { //Khoi tao hien thi cac element thuoc Frame 4
			Cocaro.mainStage_mc.gotoAndStop(4);
			//{ Control Button
			//Cocaro.mainStage_mc.Box_btn.addEventListener(MouseEvent.CLICK, BoxFunction.Box_Click) ;
			Cocaro.mainStage_mc.Mail_btn.addEventListener(MouseEvent.CLICK, MailFunction.Mail_Click) ;
			//Cocaro.mainStage_mc.Group_btn.addEventListener(MouseEvent.CLICK, GroupFunction.Group_Click) ;
			Cocaro.mainStage_mc.Bank_btn.addEventListener(MouseEvent.CLICK, BankFunction.Bank_Click) ;
			//Cocaro.mainStage_mc.Shop_btn.addEventListener(MouseEvent.CLICK, ShopFunction.Shop_Click) ;
			//Cocaro.mainStage_mc.Football_btn.addEventListener(MouseEvent.CLICK, FootballFunction.Football_Click) ;
			//Cocaro.mainStage_mc.Help_btn.addEventListener(MouseEvent.CLICK, HelpFunction.Help_Click) ;
			//Cocaro.mainStage_mc.Quest_btn.addEventListener(MouseEvent.CLICK, QuestFunction.Quest_Click) ;
			//Cocaro.mainStage_mc.Lock_btn.addEventListener(MouseEvent.CLICK, LockFunction.Lock_Click) ;
			//Cocaro.mainStage_mc.Profile_btn.addEventListener(MouseEvent.CLICK, ProfileFunction.Profile_Click) ;
			if (Cocaro.currPlayer.CountMsg > 0) {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.text = Cocaro.currPlayer.CountMsg ;
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = true ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(2) ;
			} else {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = false ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(1) ;
			}
			ChatFunction.InitChat_OutTable() ;
			
			Cocaro.mainStage_mc.btnCreateTable.addEventListener(MouseEvent.CLICK, clickCreateTable) ;			
			Cocaro.mainStage_mc.btnComeInTable.addEventListener(MouseEvent.CLICK, clickComeInTable) ;
			Cocaro.mainStage_mc.btnPlayGame.addEventListener(MouseEvent.CLICK, btnPlayGame) ;			
			Cocaro.mainStage_mc.btnBack_mc.addEventListener(MouseEvent.CLICK, btnBack_mc_Click);
			//Cocaro.mainStage_mc.btnPlayGame.visible = false ;
			if (Cocaro.isBegin) {
				Cocaro.mainStage_mc.btnCreateTable.visible = false ;
				Cocaro.mainStage_mc.btnComeInTable.visible = false ;				
				Cocaro.mainStage_mc.btnPlayGame.visible = false ;
			}			
			//}
			////Cocaro.tf.appendText("==>1") ;
			//{ Hien thi thong tin nguoi choi
			if ((Cocaro.currPlayer.TinyURL != "") && (Cocaro.currPlayer.TinyURL != null)) {
				var imageLoader:Loader = new Loader();
				var image:URLRequest = new URLRequest(Cocaro.currPlayer.TinyURL) ;
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
				imageLoader.name = "BigAW" ;
				imageLoader.load(image);
				function onLoader(evt:Event)
				{
					imageLoader.width = 50 ;
					imageLoader.height = 50 ;
					if (Cocaro.mainStage_mc.Profile_mc.Avatar_mc.numChildren > 1){
						Cocaro.mainStage_mc.Profile_mc.Avatar_mc.removeChildAt(1) ;
					}
					Cocaro.mainStage_mc.Profile_mc.Avatar_mc.addChild(imageLoader);
				}				
			}
			////Cocaro.tf.appendText("==>2") ;
			Cocaro.mainStage_mc.Profile_mc.Name_txt.text = Cocaro.currPlayer.PlayerName ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG" ;			
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ" ;			
			Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatTextMain) ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;			
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatTextMain) ;
			Cocaro.mainStage_mc.Profile_mc.Vip_mc.visible = true ;
			Cocaro.mainStage_mc.Profile_mc.Vip_mc.gotoAndStop(Cocaro.currPlayer.VIP + 1) ;
			var positionKhung : int = 1 ;			
			if (Cocaro.currPlayer.Mark > Cocaro.vip) {
				positionKhung = 3 ;
				Cocaro.formatAvatar_Name.color = 0xFFFFFF ;
			} else {
				if (Cocaro.currPlayer.Mark > Cocaro.vipSilver)
					positionKhung = 2 ;
				Cocaro.formatAvatar_Name.color = 0xE5DFE9 ;
			}
			Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.Profile_mc.BG_Avatar.gotoAndStop(positionKhung) ;
			//}
			////Cocaro.tf.appendText("==>3") ;
			//{ Check Tien hien Popup
			//if ((Cocaro.currPlayer.Mark < 500) && (Cocaro.numberWarning < 2)) {
				//Cocaro.numberWarning = Cocaro.numberWarning + 1 ;
				//var tmpTB:MovieClip = MovieClip(new Popup_TangAG()) ;
				//if (Cocaro.currPlayer.Mark == 0)
					//tmpTB.txtThongbao.text = "Bạn đã hết AG, Bạn có muốn nạp AG ?"	 ;
				//else 
					//tmpTB.txtThongbao.text = "Bạn đã gần hết AG, Bạn có muốn nạp thêm AG ?"	 ;
				//tmpTB.NapAG_btn.addEventListener(MouseEvent.CLICK, Cocaro.NapAG_Popup_Click) ;
				//tmpTB.Choitiep_btn.addEventListener(MouseEvent.CLICK, Cocaro.Choitiep_Popup_Click) ;
				//tmpTB.name = "TangAG" ;
				//tmpTB.x = 0;
				//tmpTB.y = 0;
				//Cocaro.mainStage_mc.addChild(tmpTB) ;
			//}
			//}
		}
		
		public static function initRoom(str:String):void { //Add 1 Room vao giao dien
			//Cocaro.tf.appendText("=>Room:" + str) ;
			if (Cocaro.mainStage_mc.currentFrame != 4) {
				movieRooms = new Array() ;
				TaoKhungFrame2();
			}
			if (movieRooms == null) movieRooms = new Array() ;			
			var object = JSON.decode(str) ; 
			if (object == null) return ;
			if (movieRooms.length > 5) return ;
			for (var i:int = 0; i < object.length; i++) 
			{
				if (Cocaro.mainStage_mc.ListRoom_mc.getChildByName("Room:" + object[i].Id) != null) continue ;
				var or:ObjectRoom = new ObjectRoom();
				or.RoomID = object[i].Id;
				or.RoomName = object[i].Name;
				or.MaxTable = object[i].MaxTable ;
				or.CurrPlayer = object[i].CurPlay ;
				or.MaxPlayer = object[i].MaxPlay ;
				or.CurrTable = object[i].CurTable ;
				var _mc :MovieClip = MovieClip(new Room()) ;	
				_mc.name = "Room:" + object[i].Id ;
				_mc.Bg_Normal.visible = true ;
				_mc.Bg_Over.visible = false ;
				_mc.Bg_Active.visible = false ;
				_mc.x = movieRooms.length * 155 ;
				_mc.y = 0 ;				
				/*if ((Cocaro.currRoom != null) && (!Cocaro.isBegin)) {
					if (object.Id == Cocaro.currRoom.RoomID)
						_mc.btnBg_mc.gotoAndStop(1) ;				
				}*/
				movieRooms.push(_mc) ;
				new RoomController(_mc, or) ;
				Cocaro.mainStage_mc.ListRoom_mc.addChild(_mc) ;
			}
			//var cs : int = Math.floor(Math.random() * object.length) ;
			//clickRoom(object[cs].Id) ;
			for (var j:int = 0; j < object.length; j++) 
			{
				if (object[j].CurPlay < object[j].MaxPlay) {
					clickRoom(object[j].Id) ;
					break ;
				}
			}		
		}
		
		public static function clickRoom(_id:int):void {
			Cocaro.SoundKick() ;
			var ob:Object = new Object();
			ob.id = _id;
			ob.evt = "3";
			ob.gameid = Cocaro.GAME_ID;
			Cocaro.mainSocket.sendService(JSON.encode(ob));			
			if (Cocaro.currRoom == null) 
				Cocaro.currRoom = new ObjectRoom() ;
			Cocaro.currRoom.RoomID = _id ;	
			Cocaro.roomID = _id ;
			if (Cocaro.isBegin) {
				Cocaro.mainStage_mc.btnCreateTable.visible = true ;
				Cocaro.mainStage_mc.btnComeInTable.visible = true ;
				Cocaro.mainStage_mc.btnPlayGame.visible = true ;
				Cocaro.isBegin = false ;
			}
			//Khoi dong
			movieTables = new Array() ;
			movieTablesXY = new Array() ;
			scrollPaneTable_mc = new MovieClip() ;
			scrTable.source = scrollPaneTable_mc; 
			scrTable.setSize(740, 440); 			
			scrTable.horizontalScrollPolicy = ScrollPolicy.OFF ;
			scrTable.verticalScrollPolicy = ScrollPolicy.AUTO ;
			scrTable.move(0, 0);			
			Cocaro.mainStage_mc.ListTable_mc.addChild(scrTable) ;			
			//Khoi tao vien sang			
			for (var i:int = 0; i < movieRooms.length; i++) 
			{
				if (movieRooms[i].name == "Room:" + _id) {
					movieRooms[i].Bg_Normal.visible = false ;
					movieRooms[i].Bg_Over.visible = false ;
					movieRooms[i].Bg_Active.visible = true ;
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Tahoma" ;
					formatText.bold = true ;
					formatText.size = 12 ;
					formatText.color = 0x000000 ;
					movieRooms[i].header_txt.setTextFormat(formatText) ;				
				} else {
					movieRooms[i].Bg_Normal.visible = true ;
					movieRooms[i].Bg_Over.visible = false ;
					movieRooms[i].Bg_Active.visible = false ;
					var formatText : TextFormat = new TextFormat() ;				
					formatText.font = "Tahoma" ;
					formatText.bold = true ;
					formatText.size = 12 ;
					formatText.color = 0xFFFFFF ;
					movieRooms[i].header_txt.setTextFormat(formatText) ;
				}
			}			
		}
		
		
		public static function initTable(object:Object):void { //Add vao danh sach table mot table tra ve						
			//Cocaro.tf.appendText("==>Init:" + object.N + "-" + object.ArrN) ;
			//Cocaro.tf.appendText("==>Init:" + object.ArrName) ;
			var ot:ObjectTable = new ObjectTable();
			ot.TableID = object.Id ;
			ot.RoomID = object.RId ;
			ot.CurrentPlayer = 1; 
			ot.TableName = object.Name + "-" + BasicFunction.numberFormat(object.MarkUnit, 0, false, true) + " AG";
			//ot.Type = object.T ;
			ot.MarkUnit = object.MarkUnit ;
			ot.Status = 0 ;
			//Cocaro.tf.appendText("=>2") ;
			//if (object.Type == 1) {
				//ot.TableName = " Nhất ăn tất" ;				
			//} else if (object.Type == 2)
				//ot.TableName = " Chơi chương" ;
			//else 
				//ot.TableName = "Chơi chương Đánh nước" ;		
			//if (ot.RoomID != Cocaro.roomID) return ;
			if (movieTables == null) movieTables = new Array() ;
			if (movieTablesXY == null) movieTables = new Array() ;
			//Cap nhat User trong ban
			for (var j:int = 0; j < movieTables.length; j++) 
			{
				var _mcUpdateParent : MovieClip = MovieClip(scrollPaneTable_mc.getChildByName("Table:" + ot.TableID)) ;				
				if (_mcUpdateParent != null) {
					var _mcUpdate : MovieClip = MovieClip(_mcUpdateParent.getChildAt(0)) ;
					for (var l:int = 0; l < 2; l++) 
						_mcUpdate["user" + l.toString() + "_txt"].text = "" ;
					for (var k:int = 0; k < object.ArrName.length; k++) 
						_mcUpdate["user" + k.toString() + "_txt"].text = object.ArrName[k] ;
					return ;
				}				
			}
			var mc:MovieClip = new MovieClip();	
			var tmpTableTemp : MovieClip ;
			var tmpTable : MovieClip ;		
			if (ot.MarkUnit >= 1000000){ 	//table vip
				tmpTableTemp = MovieClip(new TableVip());
				tmpTable = MovieClip(new TableVip());				
			} else {
				tmpTableTemp = MovieClip(new TablePlay());
				tmpTable = MovieClip(new TablePlay());
			}
			tmpTableTemp.name = "Table:" + ot.TableID ;
			//Cocaro.tf.appendText("=>1") ;
			tmpTable.name = "Table:" + ot.TableID ; //Gan Ten de ty dieu khien
			//Cocaro.tf.appendText("Name:" + object.ArrN.length) ;
			new TableController(tmpTable, ot, object.ArrName);
			var xNew : Number = 12 ;
			var yNew : Number = 8 ;
			var t : Boolean = true ;
			//Cocaro.tf.appendText("=>0") ;
			var isTD : int = 0 ;
			while (t) {
				t = false ;
				for (var i:int = 0; i < movieTablesXY.length; i++) 
				{
					if ((movieTablesXY[i].x == xNew) && (movieTablesXY[i].y == yNew)) {
						t = true ;
						break ;
					}					
				}
				if (t) {
					if (t) {
						if ((isTD == 0) || (isTD == 1) || (isTD == 2)) {
							xNew = xNew + 175;
							isTD = isTD + 1 ;
						} else {						
							yNew = yNew + 108 ;
							xNew = 12 ;
							isTD = 0
						}
					}
				}
			}
			//tmpTable.Bg_Normal.visible = true ;
			//tmpTable.Bg_Over.visible = false ;
			mc.addChild(tmpTable);
			mc.name = "Table:" + ot.TableID ;	
			mc.x = xNew ;
			mc.y = yNew ;	
			tmpTableTemp.x = xNew ;
			tmpTableTemp.y = yNew ;
			movieTables.push(tmpTable) ;	
			movieTablesXY.push(tmpTableTemp) ;
			scrollPaneTable_mc.addChild(mc) ;
			scrTable.update() ;		
			//Cocaro.tf.appendText("==>OK") ;
		}
		
		public static function DeleteTable(str : String) { //Delete mot Table
			var strID : String = "Table:" + str ;
			var _mc : MovieClip = MovieClip(scrollPaneTable_mc.getChildByName(strID)) ;			
			if (_mc != null)
				scrollPaneTable_mc.removeChild(_mc) ;			
			scrTable.update() ;
			for (var i:int = 0; i < movieTables.length; i++) 
			{				
				if (movieTables[i].name == strID) {
					movieTables.splice(i, 1) ;
					break ;
				}				
			}
			for (var j:int = 0; j < movieTablesXY.length; j++) 
			{				
				if (movieTablesXY[j].name == strID) {
					movieTablesXY.splice(j, 1) ;
					break ;
				}				
			}
		}
		
		public static function btnBack_mc_Click(evt:MouseEvent) { //Back tu Room ==> Level
			Cocaro.SoundKick() ;
			Cocaro.NonPlayMoney = 0;
			Cocaro.PlayMoney = 0;
			Cocaro.isSend = true ;
			movieRooms = null ;
			Cocaro.roomID = 0 ;
			Cocaro.isBegin = true ;
			Cocaro.htmlPrivateChat = Cocaro.mainStage_mc.Chat_mc.Private_txt.htmlText ;
			Cocaro.htmlWorldChat = Cocaro.mainStage_mc.Chat_mc.World_txt.htmlText ;
			Cocaro.htmlGameChat = Cocaro.mainStage_mc.Chat_mc.Game_txt.htmlText ;
			Cocaro.htmlAllChat = Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText ;
			//{Tro ve man hinh chon Level
			Cocaro.mainStage_mc.gotoAndStop(3);			
			//{Hien thi thong tin nguoi choi
			if ((Cocaro.currPlayer.TinyURL != "") && (Cocaro.currPlayer.TinyURL != null)) {
				var imageLoader:Loader = new Loader();
				var image:URLRequest = new URLRequest(Cocaro.currPlayer.TinyURL) ;
				imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoader);				
				imageLoader.name = "BigAW" ;
				imageLoader.load(image);				
				function onLoader(evt:Event)
				{
					imageLoader.width = 50 ;
					imageLoader.height = 50 ;
					if (Cocaro.mainStage_mc.Profile_mc.Avatar_mc.numChildren > 1)
						Cocaro.mainStage_mc.Profile_mc.Avatar_mc.removeChildAt(1) ;
					Cocaro.mainStage_mc.Profile_mc.Avatar_mc.addChild(imageLoader);
				}				
			}
			Cocaro.mainStage_mc.Profile_mc.Name_txt.text = Cocaro.currPlayer.PlayerName ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.Mark, 0, false, true) + " AG";		
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.text = BasicFunction.numberFormat(Cocaro.currPlayer.MarkLQ, 0, false, true) + " LQ";		
			Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatTextMain) ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatTextMain) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatTextMain) ;			
			Cocaro.mainStage_mc.Profile_mc.Vip_mc.visible = true ;
			Cocaro.mainStage_mc.Profile_mc.Vip_mc.gotoAndStop(Cocaro.currPlayer.VIP + 1) ;
			var positionKhung : int = 1 ;
			if (Cocaro.currPlayer.Mark > Cocaro.vip) {
				positionKhung = 3 ;
				Cocaro.formatAvatar_Name.color = 0xFFFFFF ;
			} else {
				if (Cocaro.currPlayer.Mark > Cocaro.vipSilver)
					positionKhung = 2 ;
				Cocaro.formatAvatar_Name.color = 0xE5DFE9 ;
			}
			Cocaro.mainStage_mc.Profile_mc.Name_txt.setTextFormat(Cocaro.formatAvatar_Name) ;
			Cocaro.mainStage_mc.Profile_mc.Mark_txt.setTextFormat(Cocaro.formatAvatar_Mark) ;	
			Cocaro.mainStage_mc.Profile_mc.MarkLQ_txt.setTextFormat(Cocaro.formatAvatar_MarkLQ) ;
			Cocaro.mainStage_mc.Profile_mc.BG_Avatar.gotoAndStop(positionKhung) ;
			Cocaro.PlayMoney = Cocaro.currPlayer.Mark;
			Cocaro.NonPlayMoney = Cocaro.currPlayer.Mark;
			//}
			//{Control Button
			Cocaro.mainStage_mc.Error_btn.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.Error_Click) ;
			Cocaro.mainStage_mc.GoldDreamCity_btn.addEventListener(MouseEvent.CLICK, OtherFunction.GoldDreamCity_Click) ;
			Cocaro.mainStage_mc.LuckyCard_btn.addEventListener(MouseEvent.CLICK, LuckyCardFunction.LuckyCard_Click) ;
			Cocaro.mainStage_mc.DreamCity_btn.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.DreamCity_Click) ;
			Cocaro.mainStage_mc.Auction_btn.addEventListener(MouseEvent.CLICK, AuctionFunction.Auction_Click) ;
			Cocaro.mainStage_mc.Lottery_btn.addEventListener(MouseEvent.CLICK, LotteryFunction.Lottery_Click) ;
			Cocaro.mainStage_mc.Mail_btn.addEventListener(MouseEvent.CLICK, MailFunction.Mail_Click) ;
			//Cocaro.mainStage_mc.Group_btn.addEventListener(MouseEvent.CLICK, GroupFunction.Group_Click) ;
			Cocaro.mainStage_mc.Bank_btn.addEventListener(MouseEvent.CLICK, BankFunction.Bank_Click) ;
			//Cocaro.mainStage_mc.Shop_btn.addEventListener(MouseEvent.CLICK, ShopFunction.Shop_Click) ;
			//Cocaro.mainStage_mc.Football_btn.addEventListener(MouseEvent.CLICK, FootballFunction.Football_Click) ;
			//Cocaro.mainStage_mc.Help_btn.addEventListener(MouseEvent.CLICK, HelpFunction.Help_Click) ;
			//Cocaro.mainStage_mc.Quest_btn.addEventListener(MouseEvent.CLICK, QuestFunction.Quest_Click) ;
			Cocaro.mainStage_mc.Lock_btn.addEventListener(MouseEvent.CLICK, LockFunction.Lock_Click) ;
			Cocaro.mainStage_mc.Profile_btn.addEventListener(MouseEvent.CLICK, ProfileFunction.Profile_Click) ;
			if (Cocaro.currPlayer.CountMsg > 0) {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.text = Cocaro.currPlayer.CountMsg ;
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = true ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(2) ;
			} else {
				Cocaro.mainStage_mc.Mail_btn.CountMsg_txt.visible = false ;
				Cocaro.mainStage_mc.Mail_btn.gotoAndStop(1) ;
			}
			
			ChatFunction.InitChat_OutTable() ;
			ProcessOutTable_Frame1.InitChangeGame() ;
			Cocaro.mainStage_mc.Friend_mc.FriendGame_btn.addEventListener(MouseEvent.CLICK, FriendFunction.FriendGame_Click) ;
			Cocaro.mainStage_mc.Friend_mc.FriendZing_btn.addEventListener(MouseEvent.CLICK, FriendFunction.FriendZing_Click) ;
			Cocaro.mainStage_mc.Friend_mc.FriendTop_btn.addEventListener(MouseEvent.CLICK, FriendFunction.FriendTop_Click) ;
			
			Cocaro.mainStage_mc.Vaoban_Btn.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.Vaoban_Click) ;
			Cocaro.mainStage_mc.btnDG.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.btnDG_mc_Click) ;
			Cocaro.mainStage_mc.btnCT.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.btnCT_mc_Click) ;
			Cocaro.mainStage_mc.btnNew.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.btnNew_mc_Click) ;
			Cocaro.mainStage_mc.btnLQAG.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.btnLQAG_Click) ;
			Cocaro.mainStage_mc.btnNapAG.addEventListener(MouseEvent.CLICK,ProcessOutTable_Frame1. btnNapAG_mc_Click) ;
			Cocaro.mainStage_mc.btnDKGiaidau.addEventListener(MouseEvent.CLICK, ProcessOutTable_Frame1.btnDKGiaidau_Click) ;
			Cocaro.mainStage_mc.btnDKGiaidau.visible = false ;
			//Cocaro.mainStage_mc.btnNapAG.visible = false ;
			//Cocaro.mainStage_mc.Auction_btn.visible = false ;
			LQAGFunction.Init() ;
			TopFunction.DisplayHotNew() ;
			//}
			//InitCTDG() ;
			/*var dtCheck:Date = new Date();
			if (dtCheck.day == 1 || dtCheck.date ==1)
			{
				Cocaro.mainStage_mc.CTDG_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.CTThang_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.CTTrongngay_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc.visible = false ;
				Cocaro.mainStage_mc.btnCT.gotoAndStop(1) ;
				Cocaro.mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTTuan_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTThang_btn.visible = false;
				Cocaro.mainStage_mc.btnDG.gotoAndStop(1) ;
				Cocaro.mainStage_mc.btnNew.gotoAndStop(2) ;
				Cocaro.mainStage_mc.New_mc.visible = true ;
				Cocaro.mainStage_mc.BGTop_mc.visible = false;
			} else {
				var Yesterday:Date = new Date();			
				Yesterday.setDate(Yesterday.getDate() - 1);
				var day1: String = Yesterday.getDate().toString();
				var month1 = (Yesterday.getMonth() + 1).toString()
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_mc.Title_txt.text = "Top Cao thủ Ba cây ngày " + day1 + "/" + month1;  				
				Cocaro.mainStage_mc.CTDG_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.visible = true ;
				Cocaro.mainStage_mc.CT_mc.CTTuanThang_mc.visible = false ;
				Cocaro.mainStage_mc.CT_mc.CTThang_mc.visible = false ;
				Cocaro.mainStage_mc.btnCT.gotoAndStop(2) ;
				Cocaro.mainStage_mc.CT_mc.CTTrongngay_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTHomtruoc_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTTuan_btn.visible = false;
				Cocaro.mainStage_mc.CT_mc.CTThang_btn.visible = false;
				Cocaro.mainStage_mc.btnDG.gotoAndStop(1) ;
				Cocaro.mainStage_mc.btnNew.gotoAndStop(1) ;
				Cocaro.mainStage_mc.New_mc.visible = false ;
				ProcessOutTable_Frame1.InitCTDG_CT();
			}*/
			/*for (var i:int = 0; i < arrLevelInfor.length; i++) 
			{
				var item:Object	 = arrLevelInfor[i];
				var oLR : ObjectLevelRoom = new ObjectLevelRoom() ;
				oLR.LevelID = item.Id ;
				oLR.LevelName = item.Name ;
				oLR.CurrPlayer = item.CurPlay ;
				oLR.MaxMark = item.MaxMark ;
				oLR.MinMark = item.MinMark ;				
				if (i == 0) {
					new LevelRoomController(mainStage_mc.level1_mc, oLR) ;
				} else if (i == 1) {
					new LevelRoomController(mainStage_mc.level2_mc, oLR) ;
				}
			}*/
			movieRooms = new Array() ;	
			//{ Kiem tra gui yeu cau Feed len tuong
			//var markChange : Number = 0 ;
			//var markS : Number ;
			//if (markInit != 0) {
				//markChange = currPlayer.Mark - markInit ;
				//if (currPlayer.Mark > 1000000) {					
					//markS = 0.3 * markInit ;
				//} else if (currPlayer.Mark > 200000) {
					//markS = 0.5 * markInit;
				//} else if (currPlayer.Mark > 20000) {
					//markS = 2 * markInit  ;
				//} else {
					//markS = 2 * markInit  ;
				//}
				//if (markChange > markS) {
					//ExternalInterface.call("pushfeed",user.UserId);
				//} else {
					//if (currPlayer.Mark > 1000000)
						//markS = 0.3 * markInit ;
					//else 
						//markS = 0.5 * markInit;
					//if (markChange < 0 - markS)
						//ExternalInterface.call("pushfeed",user.UserId);
				//}
			//}
			//ProcessOutTable_Frame1.LoadData() ;
			Cocaro.markInit = Cocaro.currPlayer.Mark ;
			//ExternalInterface.call("pushfeed", user.UserId);
			//tf.appendText(" \n init = " +InitMoney.toString());
			//tf.appendText("\n Playmoney = " + PlayMoney.toString());
			//tf.appendText("\n NonPlayMoney = " + NonPlayMoney.toString());
			/*if (NonPlayMoney == InitMoney)
			{
				ExternalInterface.call("pushfeed", user.UserId);
			}
			else if (PlayMoney - InitMoney > 0)
			{	
				ExternalInterface.call("pushfeed", user.UserId, 3 );
			}
			else if(PlayMoney - InitMoney < 0)
			{
				ExternalInterface.call("pushfeed", user.UserId, 2 );
			}*/
			//}	
			//}
		}
		public static function clickJoinTable(_id:int):void	{ //Kick vao 1 Table de join vao choi
			Cocaro.htmlPrivateChat = Cocaro.mainStage_mc.Chat_mc.Private_txt.htmlText ;
			Cocaro.htmlWorldChat = Cocaro.mainStage_mc.Chat_mc.World_txt.htmlText ;
			Cocaro.htmlGameChat = Cocaro.mainStage_mc.Chat_mc.Game_txt.htmlText ;
			Cocaro.htmlAllChat = Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText ;
			Cocaro.SoundKick();
			Cocaro.isSend = true ;
			Cocaro.mainSocket.sendJoinTableRequest(_id);
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;			
			Cocaro.mainStage_mc.addChild(tmpLD);
		}
		
		public static function btnPlayGame(evt:MouseEvent) : void { //Kick nut choi ngay
			Cocaro.SoundKick() ;
			var obj:Object = new Object();
			obj.evt = "4";
			Cocaro.mainSocket.sendService(JSON.encode(obj));	
			//Cocaro.tf.appendText("=>Play:" + JSON.encode(obj)) ;
		}
		
		public static function clickComeInTable(evt:MouseEvent) : void { //Kick nut Vao ban
			Cocaro.htmlPrivateChat = Cocaro.mainStage_mc.Chat_mc.Private_txt.htmlText ;
			Cocaro.htmlWorldChat = Cocaro.mainStage_mc.Chat_mc.World_txt.htmlText ;
			Cocaro.htmlGameChat = Cocaro.mainStage_mc.Chat_mc.Game_txt.htmlText ;
			Cocaro.htmlAllChat = Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText ;
			Cocaro.SoundKick() ;
			//Hide
			var tmpTable:MovieClip = MovieClip(new IDTable_Popup());
			tmpTable.name = "IDTable_Popup";
			tmpTable.x = 0;
			tmpTable.y = 0;
			tmpTable.addEventListener(KeyboardEvent.KEY_DOWN, keyComeInTable) ;
			tmpTable.btnXacnhan.addEventListener(MouseEvent.CLICK, btnXacnhan_ComeIn_click);
			tmpTable.btnHuybo.addEventListener(MouseEvent.CLICK, btnHuybo_ComeIn_click);			
			Cocaro.mainStage_mc.addChild(tmpTable);
		}
		public static function keyComeInTable(evt : KeyboardEvent) { //Xu ly Phim tren popup vao ban
			if (evt.keyCode == 13) {
				var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("IDTable_Popup")) ;
				var strID : String = tmp.txtID_mc.text ;
				if (strID == "") return ;
				Cocaro.mainSocket.sendJoinTableRequest(parseInt(strID));
				Cocaro.mainStage_mc.removeChild(tmp) ;
				var tmpHide:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));
				while (tmpHide != null) {
					Cocaro.mainStage_mc.removeChild(tmpHide) ;
					tmpHide = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));
				}
			}			
		}
		public static function btnXacnhan_ComeIn_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup vao ban
			Cocaro.SoundKick() ;
			var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("IDTable_Popup")) ;
			var strID : String = tmp.txtID_mc.text ;
			if (strID == "") return ;
			Cocaro.mainSocket.sendJoinTableRequest(parseInt(strID));
			Cocaro.mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				Cocaro.mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));
			}
		}		
		public static function btnHuybo_ComeIn_click(evt:MouseEvent) {//Xu ly Kick Huy bo tren popup vao ban
			Cocaro.SoundKick() ;
			var tmp :MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("IDTable_Popup")) ;
			Cocaro.mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				Cocaro.mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));
			}
		}
		
		public static function clickCreateTable(evt:MouseEvent):void	{ //Tao ban
			Cocaro.htmlPrivateChat = Cocaro.mainStage_mc.Chat_mc.Private_txt.htmlText ;
			Cocaro.htmlWorldChat = Cocaro.mainStage_mc.Chat_mc.World_txt.htmlText ;
			Cocaro.htmlGameChat = Cocaro.mainStage_mc.Chat_mc.Game_txt.htmlText ;
			Cocaro.htmlAllChat = Cocaro.mainStage_mc.Chat_mc.All_txt.htmlText ;
			Cocaro.SoundKick() ;
			var tmpTable:MovieClip = MovieClip(new Table_Popup());
			tmpTable.name = "Table_Popup";
			var formatText : TextFormat = new TextFormat() ;				
			formatText.font = "Tahoma" ;
			formatText.bold = true ;
			formatText.color = 0xFFFF00 ;
			formatText.size = 12 ;
			tmpTable.rb1.setStyle("textFormat", formatText) ;
			tmpTable.rb2.setStyle("textFormat", formatText) ;
			tmpTable.rb3.setStyle("textFormat", formatText) ;
			tmpTable.rb4.setStyle("textFormat", formatText) ;
			tmpTable.rb5.setStyle("textFormat", formatText) ;
			tmpTable.rb6.setStyle("textFormat", formatText) ;
			tmpTable.TimePer1.setStyle("textFormat", formatText) ;
			tmpTable.TimePer2.setStyle("textFormat", formatText) ;
			tmpTable.TotalTime1.setStyle("textFormat", formatText) ;
			tmpTable.TotalTime2.setStyle("textFormat", formatText) ;
			tmpTable.name_txt.text = "Kiếm tiền đê!" ;
			var tfStyle:TextFormat = new TextFormat();
			tfStyle.size = 12;
			tfStyle.color = 0xFFFFFF;
			tfStyle.font = "Arial";
			tmpTable.name_txt.setStyle("textFormat", tfStyle);
			if (Cocaro.currLevel.MinMark == 0) {
				tmpTable.rb1.label = "1,000 AG" ;
				tmpTable.rb1.value = 1000 ;
				tmpTable.rb1.enabled = true ;
				tmpTable.rb2.label = "10,000 AG" ;
				tmpTable.rb2.value = 10000 ;
				tmpTable.rb2.enabled = true ;
				tmpTable.rb3.label = "100,000 AG" ;
				tmpTable.rb3.value = 100000 ;
				tmpTable.rb3.enabled = true ;
				tmpTable.rb4.label = "500,000 AG" ;
				tmpTable.rb4.value = 500000 ;
				tmpTable.rb4.enabled = true ;
				tmpTable.rb5.label = "1,000,000 AG" ;
				tmpTable.rb5.value = 1000000 ;
				tmpTable.rb5.enabled = true ;
				tmpTable.rb6.label = "5,000,000 AG" ;
				tmpTable.rb6.value = 5000000 ;
				tmpTable.rb6.enabled = true ;
				tmpTable.rb2.selected = true ;
			}
			tmpTable.TimePer1.enabled = true ;
			tmpTable.TimePer2.enabled = true ;
			tmpTable.TotalTime1.selected = true ;
			tmpTable.TotalTime2.selected = true ;
			tmpTable.x = 0;
			tmpTable.y = 0;
			tmpTable.addEventListener(KeyboardEvent.KEY_DOWN, keyPopupTable) ;			
			tmpTable.btnOK_mc.addEventListener(MouseEvent.CLICK, btnOK_mc_click);
			tmpTable.btnNotOK_mc.addEventListener(MouseEvent.CLICK, btnNotOK_mc_click);
			Cocaro.mainStage_mc.addChild(tmpTable);			
		}
		
		public static function btnRule_mc_click(evt : MouseEvent) {
			var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Table_Popup")) ;
			var rbRule : Object = tmp.Rule1.group ;
			if (rbRule.selection.value == 2) {
				tmp.chkDanhnuoc.visible = true ;
				tmp.chkDanhnuoc.selected = true ;
			} else {
				tmp.chkDanhnuoc.visible = false ;
			}
		}
		
		public static function keyPopupTable(evt : KeyboardEvent) { //Xu ly Phim tren popup tao ban
			if (evt.keyCode == 13) {
				var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Table_Popup")) ;
				var strName : String = tmp.name_txt.text ;			
				var rbg : Object = tmp.rb1.group ;
				var rbTimePer : Object = tmp.TimePer1.group ;
				var rbTotalTime : Object = tmp.TotalTime1.group ;
				if (rbg.selection == null) return ;
				if (rbTimePer.selection == null) return ;
				if (rbTotalTime.selection == null) return ;
				if (strName.length > 100)
					strName = strName.substring(0, 100) ;
				strName = BasicFunction.trim(strName) ;
				if (strName.length < 1) return ;
				//obj.evt = "ctable";				
				var requestTablePacket:CreateTableRequestPacket = new CreateTableRequestPacket();
				requestTablePacket.gameid = Cocaro.GAME_ID;
				requestTablePacket.seats = 2;
				requestTablePacket.seq = 1;
				requestTablePacket.invitees = new Array();
				var gameid:Param = ParameterUtil.makeIntegerParam("gameId", Cocaro.GAME_ID);
				var name:Param = ParameterUtil.makeStringParam("Name", strName);
				var type:Param = ParameterUtil.makeIntegerParam("Type", 1);
				var mark:Param = ParameterUtil.makeIntegerParam("Mark", rbg.selection.value);
				var timeper:Param = ParameterUtil.makeIntegerParam("TimePer", rbTimePer.selection.value);
				var totaltime:Param = ParameterUtil.makeIntegerParam("TotalTime", rbTotalTime.selection.value);
				requestTablePacket.params = [gameid, type, mark, timeper, totaltime, name];
				Cocaro.mainSocket.send(requestTablePacket);
				
				Cocaro.mainStage_mc.removeChild(tmp) ;
				//mainStage_mc.bgMain_mc.visible = false ;
				//Hien thi Man hinh Loading
				var tmpLD:MovieClip = MovieClip(new Loading());
				tmpLD.name = "Loading";
				tmpLD.x = 0;
				tmpLD.y = 0;			
				Cocaro.mainStage_mc.addChild(tmpLD);
			}					
		}		
		public static function btnOK_mc_click(evt:MouseEvent) { //Xu ly Kick Xac nhan tren popup tao ban
			var tmp:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Table_Popup")) ;
			var strName : String = tmp.name_txt.text ;			
			var rbg : Object = tmp.rb1.group ;
			var rbTimePer : Object = tmp.TimePer1.group ;
			var rbTotalTime : Object = tmp.TotalTime1.group ;
			if (rbg.selection == null) return ;
			if (rbTimePer.selection == null) return ;
			if (rbTotalTime.selection == null) return ;
			if (strName.length > 100)
				strName = strName.substring(0, 100) ;
			strName = BasicFunction.trim(strName) ;
			if (strName.length < 1) return ;
			//obj.evt = "ctable";
			var requestTablePacket:CreateTableRequestPacket = new CreateTableRequestPacket();
			requestTablePacket.gameid = Cocaro.GAME_ID;
			requestTablePacket.seats = 4;
			requestTablePacket.seq = 1;
			requestTablePacket.invitees = new Array();
			var gameid:Param = ParameterUtil.makeIntegerParam("gameId", Cocaro.GAME_ID);
			var name:Param = ParameterUtil.makeStringParam("Name", strName);
			var type:Param = ParameterUtil.makeIntegerParam("Type", 1);
			var mark:Param = ParameterUtil.makeIntegerParam("Mark", rbg.selection.value);
			var timeper:Param = ParameterUtil.makeIntegerParam("TimePer", rbTimePer.selection.value);
			var totaltime:Param = ParameterUtil.makeIntegerParam("TotalTime", rbTotalTime.selection.value);
			requestTablePacket.params = [gameid, type, mark, timeper, totaltime, name];
			Cocaro.mainSocket.send(requestTablePacket);
			
			Cocaro.mainStage_mc.removeChild(tmp) ;
			//Hien thi Man hinh Loading
			var tmpLD:MovieClip = MovieClip(new Loading());
			tmpLD.name = "Loading";
			tmpLD.x = 0;
			tmpLD.y = 0;			
			Cocaro.mainStage_mc.addChild(tmpLD);		
			Cocaro.SoundKick() ;
		}
		public static function btnNotOK_mc_click(evt:MouseEvent) { //Xu ly Kick Huy bo tren popup tao ban
			var tmp :MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Table_Popup")) ;
			Cocaro.mainStage_mc.removeChild(tmp) ;
			var tmpHide:MovieClip = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));			
			while (tmpHide != null) {
				Cocaro.mainStage_mc.removeChild(tmpHide) ;
				tmpHide = MovieClip(Cocaro.mainStage_mc.getChildByName("Hide"));
			}
			Cocaro.SoundKick() ;
		}
		public static function btnKhoeBan_mc_Click(evt : MouseEvent) { //Chuyen AG
			Cocaro.Feed() ;
		}
	}
}